#include <iostream>
using namespace std;
#include "RingQueue.hpp"
#include <pthread.h>
#include"Task.hpp"
#include<ctime>
#include <unistd.h>
void* product(void*args)
{
    RingQueue<Task>*rq=static_cast<RingQueue<Task>*>(args);
    int len=opers.size();
    while(true)
    {
        //1.数据从何来 2. 数据到哪去？
      int x=rand()%10+1;
      int y=rand()%10;
      Task t(x,y,opers[rand()%len]);
      rq->push(t);
      cout<<" product a task success!! task is : "<<t.getTask()<<" tid is: ";
        printf("0x%x\n",pthread_self());
        sleep(1);
    }
    return nullptr;
}
void* consume(void*args)
{
    RingQueue<Task>*rq=static_cast<RingQueue<Task>*>(args);
    //1.数据从何而来 2.数据到哪去？
    while(true)
    {
        Task t=rq->pop();
        t();//处理数据
        cout<<" consum a task done!!task rusult is:"<<t.getResult()<<" | tid is： ";
        printf("0x%x\n",pthread_self());

        sleep(1);
    }

    return nullptr;
}
int main()
{
    srand(time(nullptr));
    RingQueue<Task>*rq=new RingQueue<Task>(20);
    pthread_t p[2],c[2];
    for(int i=0;i<2;i++)
    {
    pthread_create(p+i,nullptr,product,rq);
    pthread_create(c+i,nullptr,consume,rq);
    }
    for(int i=0;i<2;i++)
    {
    pthread_join(p[i],nullptr);
    pthread_join(c[i],nullptr);
    }

    return 0;
}